package cn.elwy.rcp.ui.composite;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import cn.elwy.rcp.ui.AppImage;

/**
 * 文件描述：分页组件
 * 
 * @author HSQ
 * @param <T>
 */
public class PageComposite<T> extends Composite {

  private List<PageListener> pageListeners = new LinkedList<PageListener>();

  // private Composite this; // 显示页面面板
  private Composite composite; // 父面板
  private ToolItem firstToolItem;
  private ToolItem preToolItem;
  private ToolItem nextToolItem;
  private ToolItem lastToolItem;
  private Label messageLabel;
  private Label totalPageLabel;
  private Text pageNumberText;

  private List<T> list; // 记录列表
  private int currentPage; // 当前页码
  private int pageSize; // 每页数目
  private int recordCount; // 总记录数
  private int totalPage; // 总面数
  private int style; // 分页样式
  private Label label_1;
  private ToolBar toolBar;
  private ToolItem tltmNewItem;
  private ToolItem tltmNewItem_1;
  private ToolItem tltmNewItem_3;
  private ToolItem tltmNewItem_2;

  public int getCurrentPage() {
    return currentPage;
  }

  public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
  }

  /**
   * Create the composite.
   * 
   * @param parent
   * @param style
   */
  public PageComposite(Composite parent, int style) {
    super(parent, style);
    GridLayout gridLayout = new GridLayout(7, false);
    gridLayout.marginHeight = 0;
    setLayout(gridLayout);

    messageLabel = new Label(this, SWT.NONE);
    messageLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));

    final ToolBar leftToolBar = new ToolBar(this, SWT.FLAT);
    firstToolItem = new ToolItem(leftToolBar, SWT.PUSH);
    // firstToolItem.setText("首页");
    firstToolItem.setImage(AppImage.PAGE_FIRST);

    preToolItem = new ToolItem(leftToolBar, SWT.PUSH);
    // preToolItem.setText("上一页");
    preToolItem.setImage(AppImage.PAGE_BACK);

    Label label = new Label(this, SWT.NONE);
    label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
    label.setText("第");

    pageNumberText = new Text(this, SWT.BORDER);
    pageNumberText.addVerifyListener(new VerifyListener() {
      public void verifyText(VerifyEvent e) {
      }
    });
    pageNumberText.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        String page = pageNumberText.getText();
        try {
          int pageNo = Integer.parseInt(page);
          if (pageNo < 0 || pageNo > totalPage) {
            MessageDialog.openError(Display.getDefault().getActiveShell(), "系统提示信息", "输入的页数超过可选范围，请重新输入");
            pageNumberText.selectAll();
            pageNumberText.forceFocus();
          } else {
            goPage(pageNo);
          }
        } catch (Exception ex) {
          MessageDialog.openError(Display.getDefault().getActiveShell(), "系统提示信息", "请输入合法的数值");
          pageNumberText.selectAll();
          pageNumberText.forceFocus();
        }

      }

      private void goPage(int pageNo) {
        // TODO Auto-generated method stub

      }
    });
    pageNumberText.setEnabled(false);
    GridData gd = new GridData();
    gd.widthHint = 30;
    gd.heightHint = 12;
    pageNumberText.setLayoutData(gd);

    totalPageLabel = new Label(this, SWT.NONE);
    totalPageLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
    totalPageLabel.setText("页/0页");

    final ToolBar rightToolBar = new ToolBar(this, SWT.FLAT);
    nextToolItem = new ToolItem(rightToolBar, SWT.PUSH);
    // nextToolItem.setText("下一页");
    nextToolItem.setImage(AppImage.PAGE_NEXT);

    lastToolItem = new ToolItem(rightToolBar, SWT.PUSH);
    // lastToolItem.setText("尾页");
    lastToolItem.setImage(AppImage.PAGE_LAST);

    label_1 = new Label(this, SWT.NONE);
    label_1.setText("共 0 条");

    // firstToolItem.setEnabled(false);
    // preToolItem.setEnabled(false);
    nextToolItem.setEnabled(false);
    lastToolItem.setEnabled(false);

    /*
     * setLayout(new FormLayout()); ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT); FormData fd_toolBar = new
     * FormData(); fd_toolBar.top = new FormAttachment(0, 5); fd_toolBar.left = new FormAttachment(0,5);
     * toolBar.setLayoutData(fd_toolBar); Label label = new Label(this, SWT.NONE); FormData fd_label = new FormData();
     * fd_label.top = new FormAttachment(0, 14); fd_label.left = new FormAttachment(0, 169);
     * label.setLayoutData(fd_label); label.setText("第"); data = new Text(this, SWT.BORDER); FormData fd_text = new
     * FormData(); fd_text.top = new FormAttachment(0, 11); fd_text.left = new FormAttachment(0, 186);
     * data.setLayoutData(fd_text); Label label_1 = new Label(this, SWT.NONE); FormData fd_label_1 = new FormData();
     * fd_label_1.top = new FormAttachment(0, 14); fd_label_1.left = new FormAttachment(0, 264);
     * label_1.setLayoutData(fd_label_1); label_1.setText("页/0页"); Label label_2 = new Label(this, SWT.NONE); FormData
     * fd_label_2 = new FormData(); fd_label_2.top = new FormAttachment(0, 14); fd_label_2.left = new FormAttachment(0,
     * 293); label_2.setLayoutData(fd_label_2); label_2.setText("共 0 条");
     */
  }

  protected void createPagePanel(Composite parent) {
    toolBar = new ToolBar(parent, SWT.FLAT | SWT.RIGHT);
    toolBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 7, 1));

    tltmNewItem = new ToolItem(toolBar, SWT.NONE);
    tltmNewItem.setText("上一页");

    tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
    tltmNewItem_1.setText("1");

    tltmNewItem_3 = new ToolItem(toolBar, SWT.NONE);
    tltmNewItem_3.setText("2");

    tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
    tltmNewItem_2.setText("下一页");
  }

  @Override
  protected void checkSubclass() {
  }

  public PageComposite(int pageSize) {
    this(pageSize, 0, 1);
  }

  public PageComposite(int pageSize, int recordCount) {
    this(pageSize, recordCount, 1);
  }

  public PageComposite(int pageSize, int recordCount, int pageNumber) {
    super(null, SWT.None);
    setPageSize(pageSize);
    setRecordCount(recordCount);
    setPageNumber(pageNumber);
  }

  public PageComposite(int pageSize, Composite composite) {
    this(pageSize, 0, 1);
    this.composite = composite;
  }

  public PageComposite(int pageSize, int recordCount, Composite composite) {
    this(pageSize, recordCount, 1);
    this.composite = composite;
  }

  public PageComposite(int pageSize, int recordCount, Composite composite, int style) {
    this(pageSize, recordCount, 1);
    this.composite = composite;
    this.style = style;
  }

  /**
   * 获得起始记录
   * 
   * @return
   */
  public int getStart() {
    return (getPageNumber() - 1) * pageSize;
  }

  /**
   * 获得结束记录
   * 
   * @return
   */
  public int getLimit() {
    // int num = getTotalPage() - getStart();
    // if (num > pageSize) {
    return pageSize;
    // } else {
    // return num;
    // }
  }

  /**
   * 获得总页数
   * 
   * @return
   */
  public int getTotalPage() {
    return totalPage;
  }

  public void setTotalPage() {
    if (recordCount % pageSize == 0) {
      this.totalPage = recordCount / pageSize;
    } else {
      this.totalPage = recordCount / pageSize + 1;
    }
  }

  /**
   * 获得当前页
   * 
   * @return
   */
  public int getPageNumber() {
    return currentPage;
  }

  /**
   * 设置当前页
   * 
   * @param pageNumber
   */
  public void setPageNumber(int pageNumber) {
    if (pageNumber < 1 || getTotalPage() == 0) { // 当前页小于1则设置为第一页
      this.currentPage = 1;
    } else if (pageNumber > getTotalPage()) { // 当前页大于总页数设为最后一页
      this.currentPage = getTotalPage();
    } else {
      this.currentPage = pageNumber;
    }
  }

  public int getPageSize() {
    return pageSize;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public int getRecordCount() {
    return recordCount;
  }

  public void setRecordCount(int recordCount) {
    this.recordCount = recordCount;
    setTotalPage();
  }

  public List<T> getList() {
    return list;
  }

  public void setList(List<T> list) {
    this.list = list;
  }

  public int getStyle() {
    return style;
  }

  public void setStyle(int style) {
    this.style = style;
  }

  /**
   * 输出分页信息
   * 
   * @return
   */
  public void setPageInfo() {

    if (getTotalPage() == 0 || getTotalPage() == 1) {
      pageNumberText.setText("" + getTotalPage());
      totalPageLabel.setText("页/" + getTotalPage() + "页");
      pageNumberText.setEnabled(false);
      firstToolItem.setEnabled(false);
      preToolItem.setEnabled(false);
      nextToolItem.setEnabled(false);
      lastToolItem.setEnabled(false);
      return;
    }
    pageNumberText.setText("" + getPageNumber());
    totalPageLabel.setText("页/" + getTotalPage() + "页");

    if (getPageNumber() == 1) {
      firstToolItem.setEnabled(false);
      preToolItem.setEnabled(false);
      nextToolItem.setEnabled(true);
      lastToolItem.setEnabled(true);
    } else if (getPageNumber() == getTotalPage()) {
      firstToolItem.setEnabled(true);
      preToolItem.setEnabled(true);
      nextToolItem.setEnabled(false);
      lastToolItem.setEnabled(false);
    } else {
      firstToolItem.setEnabled(true);
      preToolItem.setEnabled(true);
      nextToolItem.setEnabled(true);
      lastToolItem.setEnabled(true);
    }
  }

  public Label getMessageLabel() {
    return messageLabel;
  }

  public void setMessageLabel(Label messageLabel) {
    this.messageLabel = messageLabel;
  }

  public ToolItem getFirstToolItem() {
    return firstToolItem;
  }

  public void setFirstToolItem(ToolItem firstToolItem) {
    this.firstToolItem = firstToolItem;
  }

  public ToolItem getLastToolItem() {
    return lastToolItem;
  }

  public void setLastToolItem(ToolItem lastToolItem) {
    this.lastToolItem = lastToolItem;
  }

  public ToolItem getNextToolItem() {
    return nextToolItem;
  }

  public void setNextToolItem(ToolItem nextToolItem) {
    this.nextToolItem = nextToolItem;
  }

  public Text getPageNumberText() {
    return pageNumberText;
  }

  public void setPageNumberText(Text pageNumberText) {
    this.pageNumberText = pageNumberText;
  }

  public ToolItem getPreToolItem() {
    return preToolItem;
  }

  public void setPreToolItem(ToolItem preToolItem) {
    this.preToolItem = preToolItem;
  }

  public Composite getComposite() {
    return composite;
  }

  public void setComposite(Composite composite) {
    this.composite = composite;
  }

  public void addPageListener(PageListener listener) {
    pageListeners.add(listener);
  }

  class PageListener {
    public void goPage(int page) {
    }

    public void goPage1(int page) {
    }

  }
}
